/**
 * There are code generation
 */
package com.neusoft.lohas.modules.ivm.web.product;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.neusoft.lohas.common.config.Global;
import com.neusoft.lohas.common.utils.StringUtils;
import com.neusoft.lohas.common.web.BaseController;
import com.neusoft.lohas.modules.ivm.entity.product.ProductClassify;
import com.neusoft.lohas.modules.ivm.service.product.ProductClassifyService;
import com.neusoft.lohas.modules.ivm.utils.ProductUtils;
import com.neusoft.lohas.modules.sys.entity.User;
import com.neusoft.lohas.modules.sys.utils.UserUtils;

/**
 * 商品分类Controller
 * @author Jackie.Liu
 * @version 2014-10-28
 */
@Controller
@RequestMapping(value = "${adminPath}/ivm/product/classify")
public class ProductClassifyController extends BaseController {

	@Autowired
	private ProductClassifyService productClassifyService;
	
	@ModelAttribute
	public ProductClassify get(@RequestParam(required=false) String id) {
		if (StringUtils.isNotBlank(id)){
			return productClassifyService.get(id);
		}else{
			return new ProductClassify();
		}
	}
	
	@RequiresPermissions("ivm:product:classify:view")
	@RequestMapping(value = {"list", ""})
	public String list(ProductClassify productClassify, HttpServletRequest request, HttpServletResponse response, Model model) {
		User user = UserUtils.getUser();
		if (!user.isAdmin()){
			productClassify.setCreateBy(user);
		}
        List<ProductClassify> list = ProductUtils.getClassifyList();
        for (ProductClassify bean : list) {
			if ((bean.getParent()==null || StringUtils.isBlank(bean.getParent().getId())) && StringUtils.isBlank(bean.getShopUrl())) {
				bean.setShopUrl(frontPath+"?sid="+super.merchant.getId()+"&classifyId="+bean.getId()+"&shopModel=classify");
			}
		}
        model.addAttribute("list", list);
		return "modules/ivm/set/productClassifyList";
	}

	@RequiresPermissions("ivm:product:classify:view")
	@RequestMapping(value = "form")
	public String form(ProductClassify productClassify, Model model) {
		if (StringUtils.isBlank(productClassify.getId())) {
			productClassify.setMerchant(super.merchant);
		}
		if ((productClassify.getParent()==null || StringUtils.isBlank(productClassify.getParent().getId())) && StringUtils.isNotBlank(productClassify.getId()) && StringUtils.isBlank(productClassify.getShopUrl())) {
			productClassify.setShopUrl(frontPath+"?sid="+super.merchant.getId()+"&classifyId="+productClassify.getId()+"&shopModel=classify");
		}
		model.addAttribute("productClassify", productClassify);
		return "modules/ivm/set/productClassifyForm";
	}

	@RequiresPermissions("ivm:product:classify:edit")
	@RequestMapping(value = "save")
	public String save(ProductClassify productClassify, String oldName, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, productClassify)){
			return form(productClassify, model);
		}
		if (!"true".equals(checkName(productClassify.getMerchant().getId(),productClassify.getName(),oldName))){
			addMessage(model, "保存商品分类'" + productClassify.getName() + "'失败，商品分类已存在");
			return form(productClassify, model);
		}
		if ((productClassify.getParent()==null || StringUtils.isBlank(productClassify.getParent().getId())) && StringUtils.isNotBlank(productClassify.getId())) {
			productClassify.setShopUrl(frontPath+"?sid="+super.merchant.getId()+"&classifyId="+productClassify.getId()+"&shopModel=classify");
		}
		productClassifyService.save(productClassify);
		addMessage(redirectAttributes, "保存商品分类'" + productClassify.getName() + "'成功");
		return "redirect:"+Global.getAdminPath()+"/ivm/product/classify/?repage";
	}
	
	@RequiresPermissions("ivm:product:classify:edit")
	@RequestMapping(value = "delete")
	public String delete(String id, RedirectAttributes redirectAttributes) {
		ProductClassify classify = productClassifyService.get(id);
		if(classify.getLevel().equals("1")){//删除的分类属于一级分类
			productClassifyService.deleteChildrens(id);
		}
		productClassifyService.delete(id);
		addMessage(redirectAttributes, "删除商品分类成功");
		return "redirect:"+Global.getAdminPath()+"/ivm/product/classify/?repage";
	}
	
	@ResponseBody
	@RequiresPermissions("ivm:product:classify:edit")
	@RequestMapping(value = "checkName")
	public String checkName(String mId, String name, String oldName) {
		if (name !=null && name.equals(oldName)) {
			return "true";
		} else if (name !=null && productClassifyService.findByName(name, mId) == null) {
			return "true";
		}
		return "false";
	}
	
	/**
	 * 批量修改分类排序
	 */
	@RequiresPermissions("ivm:product:classify:edit")
	@RequestMapping(value = "updateSort")
	public String updateSort(String[] ids, Integer[] sorts, RedirectAttributes redirectAttributes) {
		if(Global.isDemoMode()){
			addMessage(redirectAttributes, "演示模式，不允许操作！");
			return "redirect:"+Global.getAdminPath()+"/ivm/product/classify/";
		}
    	int len = ids.length;
    	ProductClassify[] units = new ProductClassify[len];
    	for (int i = 0; i < len; i++) {
    		units[i] = productClassifyService.get(ids[i]);
    		units[i].setSort(sorts[i]);
    		units[i].getParent();
    		productClassifyService.save(units[i]);
    	}
    	addMessage(redirectAttributes, "保存分类排序成功!");
		return "redirect:"+Global.getAdminPath()+"/ivm/product/classify/";
	}
	
	/**
	 * '添加分类'跳转页面
	 * @param productId
	 * @param request
	 * @param response
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "showAddClassify")
	public String showAddClassify(
			ProductClassify productClassify,
			HttpServletRequest request, HttpServletResponse response, Model model) {
		productClassify.setMerchant(super.merchant);
		if ((productClassify.getParent()==null || StringUtils.isBlank(productClassify.getParent().getId())) && StringUtils.isNotBlank(productClassify.getId()) && StringUtils.isBlank(productClassify.getShopUrl())) {
			productClassify.setShopUrl(frontPath+"?sid="+super.merchant.getId()+"&classifyId="+productClassify.getId()+"&shopModel=classify");
		}
		model.addAttribute("productClassify", productClassify);
		model.addAttribute("merchant", super.merchant);
		return "modules/ivm/pro/addClassify";
	}
	
	/**
	 * 通过商品管理入口创建分类
	 * @param productClassify
	 * @param oldName
	 * @param model
	 * @param redirectAttributes
	 * @return
	 */
	@RequestMapping(value = "createClassify")
	public String createClassify(ProductClassify productClassify,
			int type, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, productClassify)){
			return form(productClassify, model);
		}
		if ((productClassify.getParent()==null || StringUtils.isBlank(productClassify.getParent().getId())) && StringUtils.isNotBlank(productClassify.getId())) {
			productClassify.setShopUrl(frontPath+"?sid="+super.merchant.getId()+"&classifyId="+productClassify.getId()+"&shopModel=classify");
		}
		productClassifyService.save(productClassify);
		addMessage(redirectAttributes, "保存商品分类'" + productClassify.getName() + "'成功");
		model.addAttribute("type", type);
		return "modules/ivm/pro/addClassify";
	}
	

}
